<?php

class Admin_Manager_Controller extends Base_Controller {

    public $restful = true;
    public $_data;

    public function __construct() {
        $this->_data = array();
        $this->model_users = new Users();
        if (!Auth::check()) {
            return Redirect::to('users/login');
        }
        /* $this->filter('before', 'csrf')->on('post');
          echo Form::token();
          echo Session::token();

         */
    }

    public function get_index() {
        $per_page = Input::get('per_page', 20);
        $this->_data["users_list"] = $this->model_users->listUsers($per_page);
        //var_dump($this->_data['users_list']);exit;
        return View::make('admin.manager.list')->with("users_list", $this->_data['users_list']);
    }

    public function get_grant() {

        $user = array('username' => '', 'email' => '', 'phone' => '', 'gender' => '');
        $uid = Input::get("uid");
        $this->_data["users"] = $this->model_users->getUsers($uid);
//		var_dump($this->_data["users"] );exit;
        if (!empty($this->_data["users"])) {
            $user['username'] = $this->_data["users"]->username;
            $user['email'] = $this->_data["users"]->email;
            $user['phone'] = $this->_data["users"]->phone;
            $user['gender'] = $this->_data["users"]->gender;
            $user['uid'] = $this->_data["users"]->id;
        }
        return View::make('admin.manager.grant')->with('user', $user);
    }

    public function post_Grant() {
        $id = Input::get('uid');
        $this->_data["vp"] = $this->validate_parameter();
        //var_dump($this->_data["vp"]);
        //var_dump(Input::get());exit;
        $this->_data["input"] = Input::get();
        $this->_data["state"] = False;
        if (!empty($id)) {
            //更新用户信息
            if ($this->_data["vp"] === True) {
                $update_array = array();
                foreach (array('email', 'phone', 'is_checked', 'gender') as $key) {
                    $value = Input::get($key);
                    if ($value)
                        $update_array[$key] = $value;
                }
                $this->_data["state"] = $this->model_users->updateUsers($id, $update_array);
            }
        }else {
            //增加一位新用户
            $password = Input::get('password');
            $confirm = Input::get('confirm');
            if ($password !== $confirm) {
                $this->_data["state"] = false;
                echo Response::json(array("state" => $this->_data['state']));
                return;
            }

            if ($this->_data["vp"] === true) {
                $input = Input::get();
                unset($input['t'], $input['confirm']);
                $input['password'] = Hash::make($input['password']);
                $this->_data["state"] = $this->model_users->addUsers($input);
            }
        }
        if ($this->_data["state"] == 1)
            $this->_data["state"] = true;
        else
            $this->_data["state"] = false;
        echo Response::json(array("state" => $this->_data['state']));
    }

    public function action_Login() {
        $this->_data['input'] = input::get();
        $credentials = array('username' => Input::get('username'), 'password' => Input::get('password'));

        if (Auth::attempt($credentials)) {
            return Redirect::to('users/profile');
        }
        //view
    }

    public function action_Logout() {
        return Auth::logout();
    }

    public function action_addUser() {

        $this->_data["vp"] = $this->validate_parameter();
        //var_dump($this->_data["vp"]);
        var_dump(Input::get());
        $this->_data["input"] = Input::get();
        $this->_data["state"] = False;
        if ($this->_data["vp"] === True) {
            $input = Input::get();
            unset($input['t']);
            $input['password'] = Hash::make($input['password']);
            $this->_data["state"] = $this->model_users->addUsers($input);
        }

        //var_dump($this->_data);
        //view
    }

    public function action_updateUsers() {
        $id = Input::get('id');
        $update_array = array();
        foreach (array('email', 'phone', 'is_checked', 'gender') as $key) {
            $value = Input::get($key);
            if ($value)
                $update_array[$key] = $value;
        }
        $this->_data["state"] = $this->model_users->updateUsers($id, $update_array);

        var_dump($this->_data);
    }

    public function post_del() {
        $id = Input::get('uid');
        $this->_data["state"] = $this->model_users->deleteUsers($id);
        if ($this->_data["state"] == 1)
            $this->_data["state"] = true;
        else
            $this->_data["state"] = false;
        echo Response::json(array("state" => $this->_data['state']));
    }

    private function action_listUsers($per_page) {
        $this->_data["users_list"] = $this->model_users->listUsers();
        return $this->_data;
        var_dump($this->_data);
    }

    public function action_getUsers() {
        $id = Input::get('id');
        $this->_data["users"] = $this->model_users->getUsers($id);
        var_dump($this->_data);
    }

    private function validate_parameter() {

        $input = Input::get();
        $rules = array(
            'username' => 'required|max:50',
//			'password' => 'required',
//			'confirm' => 'required',
            'email' => 'required',
            'phone' => 'required',
            'gender' => 'required'
        );

        $validation = Validator::make($input, $rules);

        if ($validation->fails()) {
            return $validation->errors;
        }

        return True;
    }

}
